import os, sys
mypath = os.path.dirname(__file__)
head, tail = os.path.split(mypath)
os.path.join(head, "SmallProtocol")
sys.path.insert(0, head)

from SmallProtocol.eDIP43a_Display import *



if __name__ == '__main__':
    
    
       
    try:
        display = eDIP43a_Display()
    except serial.SerialException:
        print "Error opening com port"
        exit()
    else:
        pass # just for remembering how exceptions work in python.
    


    mybuffer = bytearray()
          
    mybuffer.extend(display.eDIP_TA())  
    
    ret = display.send_Command(mybuffer)
    print ret
    
    display.protocol_Settings(pck_size= 128, timeout=200)
    
    ret, buffer = display.request_Protocol_Settings()
    
    for x in buffer:
        print hex(x)
    
    mybuffer = bytearray()

    mybuffer.extend(display.eDIP_SV())  
    
    ret = display.send_Command(mybuffer, data=False, protocol=False)
    print ret
    
    ret, buffer = display.request_Content_Buffer()
    
    print ret, buffer
    
    ret, buffer = display.repeat_Last_Package()
        
    print ret, buffer    
    

    ret, buffer = display.request_Information_Buffer()
        
    for x in buffer:
        print hex(x)
        
    mybuffer = bytearray()
    
       
    mybuffer.extend(display.eDIP_TA())   
    mybuffer.extend(display.eDIP_FD(WHITE,BLACK))
    mybuffer.extend(display.eDIP_DL())
    mybuffer.extend(display.eDIP_FZ(RED,BLACK))
    mybuffer.extend(display.eDIP_ZF(SWISS30B))
    mybuffer.extend(display.eDIP_ZW(0))
    mybuffer.extend(display.eDIP_ZC(XMAX/2, 30/4+5, "eDIP-Demo RS232|with SmallProtocoll"))
    mybuffer.extend(display.eDIP_FG(WHITE,BLACK))
    mybuffer.extend(display.eDIP_GZ(2,2))
    mybuffer.extend(display.eDIP_GD(0,30*2+7,XMAX,30*2+7))
    
    #display.send_Command(mybuffer)
    
    #mybuffer = bytearray()
    
    ## Brightness
    mybuffer.extend(display.eDIP_AQ(0))
    mybuffer.extend(display.eDIP_FZ(WHITE,BLACK))
    mybuffer.extend(display.eDIP_ZF(CHICAGO14))
    mybuffer.extend(display.eDIP_ZL(5, 30*2+15, "Adjust brightness:|by bargraph"))
    mybuffer.extend(display.eDIP_FB(GREEN,RED,WHITE))
    mybuffer.extend(display.eDIP_BM(13))
    mybuffer.extend(display.eDIP_BR(1,20,110,XMAX-20,140,5,100,1))
    mybuffer.extend(display.eDIP_YH(80))
    mybuffer.extend(display.eDIP_BA(1,80))
    mybuffer.extend(display.eDIP_AB(1))
    mybuffer.extend(display.eDIP_YB(1))
    
    #display.send_Command(mybuffer)
    
    #mybuffer = bytearray()
    
    # define Touchbuttons
    #mybuffer.extend(display.eDIP_DO(0))
    mybuffer.extend(display.eDIP_FE(BLUE,WHITE,BLUE,YELLOW,BLUE,YELLOW))
    mybuffer.extend(display.eDIP_AF(CHICAGO14))
    mybuffer.extend(display.eDIP_FA(WHITE,BLUE))
    mybuffer.extend(display.eDIP_AT(5,160,140,190,0,1,"CShow Version"))  #Button to show version as string on screen
    mybuffer.extend(display.eDIP_AT(150,160,285,190,0,2,"CDelete"))	   #Button to delete area, where Versionstring is shown
    mybuffer.extend(display.eDIP_AT(XMAX-100,72,XMAX,100,0,3,"CReset"))  #Button to rebuild scrren    
    
    display.send_Command(mybuffer)